<?php


namespace ba;


use app\api\model\Member;
use app\exceptions\ApiException;
use EasyWeChat\Factory;
use EasyWeChat\MiniProgram\Shipping\Client;

/**
 * 订单发货信息
 */
class Order
{




    public function sendOrder($data,$member)
    {

        $config = config('wechat.xcx');
        $app = Factory::officialAccount($config);
        $access_token =  $app->access_token->getToken();

        $api = "https://api.weixin.qq.com/cgi-bin/express/delivery/open_msg/trace_waybill?access_token=" . $access_token['access_token'];
        $vip_name = mb_convert_encoding($data['vip_name']['name'], 'UTF-8', 'auto');

        $data = [
            'openid' => (string)$member['openid'],
            'receiver_phone' => $data['user_phone'],
            'delivery_id' => $data['express_name'],
            'waybill_id' => $data['express_nums'],
            'goods_info' => [
                'detail_list' => [
                    'goods_name' => $vip_name,
                    'goods_img_url' =>"https://admin.tanyuanzhilv.net/storage/default/20241222/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_2024122221fd98511d23242ce5354689af7c8c9a9acf3e952c.jpg"
                ]
            ],
            'trans_id' => $data['transaction_id'],
            'order_detail_path' => 'pages/order/order'
        ];



        $res = curl_post_https($api,$data);
        $res = json_decode($res,true);
        if($res['errcode'] == 0){
            return $res['waybill_token'];
        }
        $this->error($res['errmsg']);
    }






    public function confirmReceive($data){
        $app = Factory::miniProgram(config('wechat.xcx'));
        $data = [
            'transaction_id' => $data['transaction_id'],
            'received_time' => time(),
        ];
        $result = $app->shipping->notifyConfirmReceive($data);
        if($result['errcode'] != 0){
            throw new ApiException('操作失败');
        }
    }
}
